package com.company.排序.选择排序;

import java.util.Arrays;

/**
 * @author gyhstart
 * @create 2021/5/12 - 23:45
 *
 * 选择排序法，外层循环控制所需找到最小值的次数，内层循环来寻找目标值后面比目标值小的元素，进行下标更新
 * 每次找到后，都将其放倒前面，与冒泡排序恰恰相反。
 **/
public class Sort {
    public static int[] selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int index = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[index]) {
                    index = j;
                }
            }
            int temp = arr[index];
            arr[index] = arr[i];
            arr[i] = temp;
        }
        return arr;
    }

    public static void main(String[] args) {
        int arr[] = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
        int[] selectSort = selectSort(arr);
        System.out.println("selectSort = " + Arrays.toString(selectSort));
    }
}
